package com.sandfoxstudio.GPSTest;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.format.DateFormat;
import android.util.Log;
import android.widget.Toast;

public class SQLiteDB extends SQLiteOpenHelper {
	private Context context;
	private static final String DATABASE_NAME = "geopoint.sqlite";
	public static final String ALTITUDE = "altitude";
	public static final String LONGITUDE = "longitude";
	public static final String VALUE = "value";

	public SQLiteDB(Context context) {
		super(context, DATABASE_NAME, null, 1);
		this.context = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		//make db
		Log.i("sandfox", "onCreate");
		db.execSQL("CREATE TABLE geopoint (_id INTEGER PRIMARY KEY AUTOINCREMENT, altitude DOUBLE, longitude DOUBLE, date DATETIME);");

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		//update dp
		db.execSQL("DROP TABLE IF EXISTS geopoint");
		onCreate(db);
	}

	@Override
	public void onOpen(SQLiteDatabase db) { //if db exits 
		// TODO Auto-generated method stub
		Log.i("sandfox", "onOpen");
		super.onOpen(db);
	}

	public void addData(double aLatitude, double aLongitude) {
		Log.i("sandfox", "addData");
		Toast.makeText(context, "addData: ", Toast.LENGTH_SHORT).show();
		SQLiteDatabase db = this.getWritableDatabase();
		db.execSQL("INSERT INTO geopoint (altitude, longitude) VALUES (" + aLatitude + ", " + aLongitude + ")");
	}

	public void addData(double aLatitude, double aLongitude, DateFormat aDate) {

	}

	public void deleteData() {
		SQLiteDatabase db = this.getWritableDatabase();
		db.execSQL("DELETE FROM geopoint;"); //Reset SQLite TABLE
		db.execSQL("DELETE FROM sqlite_sequence where name ='geopoint'"); //Reset SQLite TABLE

		Toast.makeText(context, "ResetDataBase", Toast.LENGTH_SHORT).show();
	}
}
